package com.lzp.badminton.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzp.badminton.model.User;
import com.lzp.badminton.mapper.UserMapper;
import com.lzp.badminton.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzp.badminton.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2022-02-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public User login(String username, String userpassword) {
        Map userMap = new HashMap();
        userMap.put("user_name", username);
        userMap.put("user_password", userpassword);
        List<User> one = userMapper.selectByMap(userMap);

        if (one.size() == 0) {
            return null;
        }
        return one.get(0);
    }

    public User findUserByUserName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", username);
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            return null;
        }
        return user;
    }

    // 获取用户全部信息
    public User getUserInfoByUserId(String userid) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userid);
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            return null;
        }
        return user;
    }

    // 获取用户头像
    public String getUserAvatarByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", username);
        User user = userMapper.selectOne(wrapper);

        if (user == null) {
            return "";
        } else {
            return user.getUserAvatar();
        }


    }

    public Boolean addUser(String username, String password) {
        // 生成唯一用户编号
        String user_id = UUID.randomUUID().toString().replaceAll("-","");
        // 获得当前时间
        String create_date = new DateUtils().getCurrentDate();

        User user = new User();
        user.setUserId(user_id);
        user.setUserName(username);
        user.setUserPassword(password);
        user.setCreateDate(create_date);

        int insert = userMapper.insert(user);

        if (insert == 1) {
            return true;
        } else {
            return false;
        }

    }

}
